<template>
  <div>
    <div style="margin-left: 20px">
      <el-form inline @submit.native.prevent="search">
        <el-form-item label="搜索" style="margin-right: 20px">
          <el-input
            v-model="searchForm.queryConditions"
            clearable
            placeholder="用户名/手机号"
            prefix-icon="el-icon-search"
            style="width: 200px"
            size="medium"
          />
        </el-form-item>
        <el-form-item label="组织" style="margin-right: 20px">
          <el-select
            v-model="searchForm.organizeId"
            clearable
            placeholder="全部"
            style="width: 200px"
            @change="organizeSelected"
          >
            <el-option
              v-for="item in organization"
              :key="item.id"
              :value="item.id"
              :label="item.organizeName"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" size="medium" type="primary">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="person_container">
      <card-person
        v-for="item in userInfo"
        :key="item.id"
        :user="item"
        class="person"
      ></card-person>

      <el-pagination
        :current-page="paging.current"
        :page-sizes="paging.pageSizes"
        :page-size="paging.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paging.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getUserInfo } from "@/api/home.js";
import { getAllOrganizationInfos } from "@/api/organization";
import CardPerson from "./card/CardPerson.vue";
export default {
  name: "PersonInfo",
  components: { "card-person": CardPerson },
  props: {
    type: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      searchForm: {
        queryConditions: "",
        organizeId: "",
        type: this.type,
      },
      userInfo: [],
      paging: {
        total: 0,
        current: 1,
        pageSizes: [4, 8, 12],
        size: 4,
      },
      organization: [],
    };
  },
  created() {
    this.getData();
    this.getOrganizeList();
  },
  methods: {
    async getOrganizeList() {
      const result = await getAllOrganizationInfos();
      this.organization = result.data;
    },
    search() {
      this.paging.current = 1;
      this.getData();
    },

    getData() {
      const form = {
        ...this.searchForm,
        page: this.paging.current,
        pageSize: this.paging.size,
      };
      getUserInfo(form)
        .then((res) => {
          this.userInfo = res.data.userInfos;
          // 返还每页显示的条数
          this.paging.total = res.data.totalCount;
        })
        .catch((err) => {
          this.$message.warning("优秀员工获取失败！");
        });
    },
    organizeSelected(val) {
      this.searchForm.organizeId = val;
      this.search();
    },
    handleSizeChange(val) {
      this.paging.current = 1;
      this.paging.size = val;
      this.getData(this.type);
    },
    handleCurrentChange(val) {
      this.paging.current = val;
      this.getData(this.type);
    },
  },
};
</script>

<style scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.btn {
  float: right;
  margin-left: 20px;
}
.person_container {
  justify-content: center;
  align-items: center;
}
.person_container .person {
  margin: 20px 40px;
  text-align: left;
}
</style>